package com.chat.datea.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chat.datea.R;
import com.chat.datea.bean.BillList;
import com.chat.datea.bean.MusicList;
import com.chat.datea.ui.MainActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class BillFragment_RecyclerView_Adapter extends RecyclerView.Adapter<BillFragment_RecyclerView_Adapter.MyViewHolder> {
    private Context context;
    private List<BillList.SongListEntity> song_list;
    private BillList billList;

    public BillFragment_RecyclerView_Adapter(Context context, List<BillList.SongListEntity> song_list, BillList billList) {
        this.context = context;
        this.song_list = song_list;
        this.billList = billList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.bill_listview_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (position < 9) {
            holder.num.setText("0" + (position + 1));
        } else {
            holder.num.setText(position + 1 + "");
        }


        switch (position) {
            case 0:
                holder.mask.setImageResource(R.mipmap.img_king_mask01);
                break;
            case 1:
                holder.mask.setImageResource(R.mipmap.img_king_mask02);
                break;
            case 2:
                holder.mask.setImageResource(R.mipmap.img_king_mask03);
                break;
            default:
                holder.mask.setImageResource(R.mipmap.img_king_mask);
                break;
        }
        if (song_list != null && song_list.size() > 0) {
            holder.music_name.setText(song_list.get(position).getTitle());
            holder.music_singer.setText(song_list.get(position).getAuthor());
            if (song_list.get(position).getHas_mv_mobile() == 0) {
                holder.mv.setVisibility(View.GONE);
            } else {
                holder.mv.setVisibility(View.VISIBLE);
            }
            if (!song_list.get(position).getPic_small().equals("")) {

                Picasso.with(context).load(song_list.get(position).getPic_small()).into(holder.music_img);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<MusicList> lists = new ArrayList<>();
                    for (BillList.SongListEntity songListEntity : song_list) {
                        MusicList musicList = new MusicList();
                        musicList.setName(songListEntity.getTitle());
                        musicList.setSinger(songListEntity.getAuthor());
                        musicList.setSongId(songListEntity.getSong_id());
                        musicList.setFrom("网络");
                        lists.add(musicList);
                    }
                    MainActivity.play(lists, position);
                    Toast.makeText(context, "请稍候!音乐正在路上", Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return song_list.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView music_name, music_singer, num;
        ImageView music_img, mask, mv, sq, more;

        public MyViewHolder(View itemView) {
            super(itemView);
            music_name = (TextView) itemView.findViewById(R.id.music_name);
            music_img = (ImageView) itemView.findViewById(R.id.music_img);
            music_singer = (TextView) itemView.findViewById(R.id.music_singer);
            num = (TextView) itemView.findViewById(R.id.num);
            mask = (ImageView) itemView.findViewById(R.id.mask);
            mv = (ImageView) itemView.findViewById(R.id.mv);
            sq = (ImageView) itemView.findViewById(R.id.sq);
            more = (ImageView) itemView.findViewById(R.id.more);

        }
    }
}
